package m.jclecai.com.fragment;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RelativeLayout;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.xrecyclerview.ProgressStyle;
import com.handmark.pulltorefresh.library.xrecyclerview.XRecyclerView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.activity.RmDetailActivity;
import m.jclecai.com.activity.RmFamousActivity;
import m.jclecai.com.adapter.RmfamousItemAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.reconmend.ReconmentRecord;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.data.Lecai_SDK;
import m.jclecai.com.data.ReceivedData;
import m.jclecai.com.listener.Callback;
import m.jclecai.com.utils.MyLog;
import m.jclecai.com.utils.http.HttpManager;

/**
 *  为比赛 比赛中 已比赛
 * Created by ZD on 2017/6/27.
 */

public class RmGameItemFragment extends BaseFragment implements
        SwipeRefreshLayout.OnRefreshListener{

    @BindView(R.id.lv_body)
    XRecyclerView lvBody;
    @BindView(R.id.rl_no_content)
    RelativeLayout rl_nocontent;
    List redPcks;
    RmfamousItemAdapter adapter;
    int flag = 0;
    ReceivedData.ReconmentRecordData reconmentRecordData = null;


    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 12345678:
                    if (reconmentRecordData != null && reconmentRecordData.status.equals("0")) {
                        redPcks.clear();
                        List<ReconmentRecord> records = reconmentRecordData.records;
                        if (null!=records&&records.size()>0&&!records.equals("[]")){
                            redPcks.addAll(reconmentRecordData.records);
                        }
                        adapter = new RmfamousItemAdapter(getActivity(),redPcks, flag);
                        adapter.setmCallback(callback);
                        lvBody.setAdapter(adapter);

                        if (redPcks.size()<=0){
                            showEmptyLayout();
                        }

                    }
                    break;
            }
        }
    };
    @Override
    public int getLayoutId() {
        return R.layout.fragment_rm;
    }

    @Override
    public void initData() {
        super.initData();
        flag = getArguments() == null ? 0 : getArguments().getInt("flag");
        redPcks = new ArrayList<>();
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        lvBody.setLayoutManager(manager);
        lvBody.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);

        adapter = new RmfamousItemAdapter(getActivity(),redPcks, flag);
        adapter.setmCallback(callback);
        lvBody.setAdapter(adapter);
        lvBody.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                //refresh data here
                lvBody.refreshComplete();
            }

            @Override
            public void onLoadMore() {
                // load more data here
                lvBody.loadMoreComplete();
            }
        });
        initLoadingLayout(lvBody);
        sendReq();
    }

    /**
     *  某个名人的推介列表
     *  flag查询条件（0,1,2）
     */
    void sendReq() {
        Map<String, String> pair = new HashMap<>();
//        pair.put("user", PreferenceUtil.getUserName(getActivity()));//Yes	string	用户名
//        pair.put("password", PreferenceUtil.getPassword(getActivity()));//Yes	string	密码
        Log.e("==persion_id ==",Lecai_SDK.persion_id+" flag "+flag);
        pair.put("flag", String.valueOf(flag));
        pair.put("id", Lecai_SDK.persion_id);
        HttpManager.postAsyn(Constant_Url.APP_USERPROMOTIONLIST_URL, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                MyLog.i(request.toString());
                showInternetOffLayout();
            }

            @Override
            public void onResponse(String response) {
                closeLoadingLayout();
                if (!TextUtils.isEmpty(response)){
                    Gson gson = new Gson();
                    reconmentRecordData = gson.fromJson(response, ReceivedData.ReconmentRecordData.class);
                    Message msg = new Message();
                    msg.what = 12345678;
                    handler.sendMessage(msg);
                }
            }
        }, pair);

    }

    @Override
    public void onRefresh() {
        sendReq();
    }

    @Override
    public void OnRetry(String btnMessage) {
        closeLoadingLayout();
        sendReq();
    }


    Callback callback = new Callback(){
        @Override
        public void callback(Object o) {
            super.callback(o);
            if(o.equals(RecommendFragment.JUMP.RmDetailActivity)){
                startActivity(new Intent(getActivity(), RmDetailActivity.class));
            }else {
                startActivity(new Intent(getActivity(), RmFamousActivity.class));
            }
        }
    };
}
